package com.apes.fn.scm.merchant.disbursement.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.util.DateUtil;
import com.apestech.framework.util.StringUtil;
import org.springframework.util.CollectionUtils;

/**
 * @Title: ConditionUtil
 * @Author fj
 * @Package com.apes.fn.scm.merchant.disbursement.utils
 * @Date 2023/10/25 15:59
 * @description: SimpleRequest条件工具类
 */
public class ConditionUtil {

    public static final String START = "Start";

    public static final String END = "End";

    /**
     * 封装SimpleRequest内Condition条件
     * 开始时间以Start结尾
     * 结算时间以End结尾
     * @param requestJO
     * @return
     */
    public static JSONObject queryConditions(JSONObject requestJO){
        JSONObject jo = new JSONObject();

        JSONArray jsonArray = requestJO.getJSONArray("condition");
        if (!CollectionUtils.isEmpty(jsonArray)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject json = (JSONObject) jsonArray.get(i);
                if (!StringUtil.isEmpty(json.getString("type")) && "date".equals(json.getString("type"))){
                    if(json.getString("operator").equals("GTE")){
                        jo.put(json.getString("field")+START, DateUtil.parse(json.getString("value"), DateUtil.SHOW_DATETIME_FORMAT));
                    }else {
                        jo.put(json.getString("field")+END, DateUtil.parse(json.getString("value"), DateUtil.SHOW_DATETIME_FORMAT));
                    }
                }else {
                    jo.put(json.getString("field"),json.getString("value"));
                }
            }
        }
        return jo;
    }

}
